package org.dromara.config.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.config.domain.vo.KyStockInTypesVo;
import org.dromara.config.domain.bo.KyStockInTypesBo;
import org.dromara.config.service.IKyStockInTypesService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 入库类型
 *
 * @author kuyu
 * @date 2023-12-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/stockInTypes/stockInTypes")
public class KyStockInTypesController extends BaseController {

    private final IKyStockInTypesService kyStockInTypesService;

    /**
     * 查询入库类型列表
     */
    @SaCheckPermission("stockInTypes:stockInTypes:list")
    @GetMapping("/list")
    public TableDataInfo<KyStockInTypesVo> list(KyStockInTypesBo bo, PageQuery pageQuery) {
        return kyStockInTypesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出入库类型列表
     */
    @SaCheckPermission("stockInTypes:stockInTypes:export")
    @Log(title = "入库类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KyStockInTypesBo bo, HttpServletResponse response) {
        List<KyStockInTypesVo> list = kyStockInTypesService.queryList(bo);
        ExcelUtil.exportExcel(list, "入库类型", KyStockInTypesVo.class, response);
    }

    /**
     * 获取入库类型详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("stockInTypes:stockInTypes:query")
    @GetMapping("/{id}")
    public R<KyStockInTypesVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(kyStockInTypesService.queryById(id));
    }

    /**
     * 新增入库类型
     */
    @SaCheckPermission("stockInTypes:stockInTypes:add")
    @Log(title = "入库类型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KyStockInTypesBo bo) {
        return toAjax(kyStockInTypesService.insertByBo(bo));
    }

    /**
     * 修改入库类型
     */
    @SaCheckPermission("stockInTypes:stockInTypes:edit")
    @Log(title = "入库类型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KyStockInTypesBo bo) {
        return toAjax(kyStockInTypesService.updateByBo(bo));
    }

    /**
     * 删除入库类型
     *
     * @param ids 主键串
     */
    @SaCheckPermission("stockInTypes:stockInTypes:remove")
    @Log(title = "入库类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(kyStockInTypesService.deleteWithValidByIds(List.of(ids), true));
    }
}
